<?php

class productController extends Controller
{

    public $initphp_list = array(
        'pro_add',
        'pro_save',
        'pro_del',
        'cat',
        'cat_add',
        'cat_save',
        'cat_del',
        'saveproduct',
        'uploadimg'
    );

    /**
     *
     * @var productService
     */
    private $product;

    public function __construct()
    {
        parent::__construct();
        $this->checkLogin();
        $this->product = InitPHP::getService("product");
    }

    public function run()
    {
        $this->view->assign("menu_now", "product_list");
        $data = $this->product->getProduct();
        $catArr = $this->product->getCatTree(false);
        $this->view->assign("cat", $catArr);
        $this->view->assign("data", $data);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/product/pro_list");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "产品管理-产品列表-凯巴斯-管理后台");
        $this->view->display();
    }

    public function pro_add()
    {
        $this->view->assign("menu_now", "product_list");
        $id = $this->controller->get_gp("id");
        if ($id > 0) {
            $data = $this->product->getOne($id);
            if ($data['has_attr']) {
                $attr = $this->product->getAttr($id);
                $this->view->assign("attr", $attr);
            }
            $this->view->assign("id", $id);
            $this->view->assign("data", $data);
        }
        $catArr = $this->product->getCatTree();
        $this->view->assign("cat", $catArr);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/product/pro_add");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "添加产品-凯巴斯-管理后台");
        $this->view->display();
    }

    public function pro_save()
    {
        $id = $this->controller->get_gp("id");
        $data = $this->controller->get_gp(array(
            'pid',
            'title',
            'seo_title',
            'model',
            'price',
            'thumb',
            'desc',
            'has_attr',
            'content',
            'is_recommand',
            'add_time'
        ));
        $data['add_time'] = strtotime($data['add_time']);
        $attr = $this->controller->get_gp("pro_attr");
        $attr_value = $this->controller->get_gp("pro_attr_value");
        if ($attr) {
            $this->product->add_attr($id, $attr, $attr_value);
            $data['has_attr'] = 1;
        } else {
            $data['has_attr'] = 0;
        }
        if (! $data['pid']) {
            JsMessage("请选择所属分类！");
            die();
        }
        if ($id) {
            $this->product->update($id, $data);
        } else {
            $this->product->add($data);
        }
        JsMessage("保存成功！", '/manage/product/run');
    }

    public function cat()
    {
        $data = $this->product->getMenu();
        $this->view->assign("menu_now", "pro_cat");
        $this->view->assign("data", $data);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/product/cat_list");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "产品分类管理-凯巴斯-管理后台");
        $this->view->display();
    }

    public function cat_add()
    {
        $id = $this->controller->get_gp("id");
        if ($id > 0) {
            $data = $this->product->getOneCat($id);
            $pid = $data['pid'];
            $this->view->assign("id", $id);
            $this->view->assign("data", $data);
        } else {
            $pid = $this->controller->get_gp("pid");
        }
        $parent = $this->product->getOneCat($pid);
        $this->view->assign("menu_now", "catgory");
        $this->view->assign("parent", $parent);
        $this->view->set_tpl("manage/header", "F"); // 设置公共头部模板
        $this->view->set_tpl("manage/product/cat_add");
        $this->view->set_tpl("manage/footer", 'L'); // 设置公共尾部模板
        $this->view->assign("title", "添加分类-凯巴斯-管理后台");
        $this->view->display();
    }

    public function pro_del()
    {
        $id = $this->controller->get_gp("id");
        $this->product->delete($id);
        JsMessage("删除成功！", "/manage/product/run");
    }

    public function cat_del()
    {
        $id = $this->controller->get_gp("id");
        $this->product->delete_cat($id);
        JsMessage("删除成功！", "/manage/product/cat");
    }

    public function cat_save()
    {
        $id = $this->controller->get_gp("id");
        $data = $this->controller->get_gp(array(
            'pid',
            'seo_title',
            'title',
            'keyword',
            'desc',
            'url',
            'sort'
        ));
        if (! $data['pid']) {
            $data['pid'] = 0;
        }
        if ($id) {
            $this->product->update_cat($id, $data);
        } else {
            $this->product->add_cat($data);
        }
        $this->saveproduct();
        JsMessage("保存成功！", '/manage/product/cat');
    }

    public function saveproduct()
    {
        $data = $this->product->getMenu(false);
        $result=array();
        foreach ($data as $k => $v) {
            $temp = array();
            $temp[] = "home/product/index";
            $temp['content'] = "home/product/details";
            $temp['parrams'] = array(
                'id' => $v['id'],
                'page' => "@page"
            );
            $result[$v['url']]=$temp;
        }
        $str = "<?php return " . var_export($result, true);
        file_put_contents(APP_PATH . "conf/product_url.php", $str . ";");
        return true;
//         JsMessage("保存成功！", '/manage/product/run');
    }

    public function uploadimg()
    {
        $appid = $this->renderArray['appid'];
        $platform = $this->renderArray['platform'];
        $path = APP_PATH . "../upload/images";
        if (! is_dir($path)) {
            _mkdir($path);
        }
        $new_name = "img_" . uniqid() . rand(1, 1000);
        $opinion = array(
            'maxSize' => 2048,
            'allowFileType' => array(
                'gif',
                'jpg',
                'png',
                'bmp'
            )
        );
        $upload = self::getUpload();
        /* 参数：表单名称；上传后新的文件名；存放的文件目录；配置，包含最大文件和文件类型 */
        $result = $upload->upload('upload', $new_name, $path, $opinion);
        if ($result['error'] == 0) {
            echo json_encode(array(
                'status' => 1,
                'data' => "/upload/images/" . $new_name . "." . $result['type']
            ));
            exit();
        } else {
            $this->error($result['msg'], '', true);
        }
    }

    /**
     *
     * @return uploadInit
     */
    private function getUpload()
    {
        return $this->getLibrary('upload'); // 文件上传类加载
    }

    /**
     * 检查登陆状态
     */
    public function checkLogin()
    {
        $session = $this->getUtil('session');
        $adminid = $session->get('adminid');
        if (empty($adminid)) {
            $function = $this->getLibrary('function');
            $function->goto_url('/manage/login');
            die();
        }
        $this->view->assign("adminname", $session->get('adminname'));
        $this->view->assign("adminid", $session->get('adminid'));
    }
}